package com.yanqu.road.dao.impl.activity.tonbing;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.tonbing.data.CrossTonBingBattlefieldRank;
import com.yanqu.road.entity.activity.tonbing.data.CrossTonBingCityWarReport;
import com.yanqu.road.entity.activity.tonbing.data.CrossTonBingUserWarReport;

import java.sql.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CrossTonBingReportDaoImpl extends TempDao {


    private CrossTonBingCityWarReport initCityReportData(ResultSet rs) throws SQLException{
        CrossTonBingCityWarReport data = new CrossTonBingCityWarReport();
        data.setActivityId(rs.getInt("activityId"));
        data.setAtkCampId(JSON.parseObject(rs.getString("atkCampId"), new TypeReference<List<Integer>>() {}));
        data.setDefCampId(rs.getInt("defCampId"));
        data.setType(rs.getInt("type"));
        data.setCityId(rs.getInt("cityId"));
        data.setAtkRankList(JSON.parseObject(rs.getString("atkRankList"), new TypeReference<LinkedList<CrossTonBingBattlefieldRank>>() {}));
        data.setDefRankList(JSON.parseObject(rs.getString("defRankList"), new TypeReference<LinkedList<CrossTonBingBattlefieldRank>>() {}));
        data.setWinCampId(rs.getInt("winCampId"));
        data.setTime(rs.getLong("time"));
        data.setMatchId(rs.getInt("matchId"));
        return data;
    }

    public Map<Integer,List<CrossTonBingCityWarReport>> getCityReportMap(int activityId){
        Map<Integer,List<CrossTonBingCityWarReport>> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_c_tonbing_city_report WHERE activityId = ? ORDER BY `time` DESC";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossTonBingCityWarReport data = initCityReportData(rs);
                if(!dataMap.containsKey(data.getCityId())){
                    dataMap.put(data.getCityId(),new ArrayList<>());
                }
                if(!dataMap.containsKey(data.getCityId())){
                    dataMap.put(data.getCityId(),new ArrayList<>());
                }
                dataMap.get(data.getCityId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public Map<Integer,List<CrossTonBingCityWarReport>> getCityReportListMap(int activityId){
        Map<Integer,List<CrossTonBingCityWarReport>> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_c_tonbing_city_report WHERE activityId = ? ORDER BY `time` ASC";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossTonBingCityWarReport data = initCityReportData(rs);
                if(!dataMap.containsKey(data.getMatchId())){
                    dataMap.put(data.getMatchId(),new ArrayList<>());
                }
                dataMap.get(data.getMatchId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }


    public boolean addCityReportList(List<CrossTonBingCityWarReport> reportList) {
        boolean result = false;
        String sql = "INSERT INTO t_c_tonbing_city_report(activityId,atkCampId,defCampId,type," +
                "cityId,atkRankList,defRankList,winCampId,time,matchId) " +
                "VALUES(?,?,?,?,?,?,?,?,?,?)" ;
        getDb().setConnName(DbNameStrategy.getCross2Db());
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (CrossTonBingCityWarReport data : reportList) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
            para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getAtkCampId())));
            para.put(i++, new DbParameter(Types.INTEGER, data.getDefCampId()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getCityId()));
            para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getAtkRankList())));
            para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDefRankList())));
            para.put(i++, new DbParameter(Types.INTEGER, data.getWinCampId()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getTime()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getMatchId()));
            mapList.add(para);
        }
        if (getDb().executeBatchQuery(sql, mapList) > -1) {
            result = true;
        }
        return result;
    }

    private CrossTonBingUserWarReport initUserReportData(ResultSet rs) throws SQLException{
        CrossTonBingUserWarReport data = new CrossTonBingUserWarReport();
        data.setActivityId(rs.getInt("activityId"));
        data.setNo(rs.getInt("no"));
        data.setUserIdDef(rs.getLong("userIdDef"));
        data.setUserIdAct(rs.getLong("userIdAct"));
        data.setDefPatronsId(rs.getInt("defPatronsId"));
        data.setActPatronsId(rs.getInt("actPatronsId"));
        data.setDefSoldierNum(rs.getLong("defSoldierNum"));
        data.setActSoldierNum(rs.getLong("actSoldierNum"));
        data.setDefAddition(rs.getInt("defAddition"));
        data.setActAddition(rs.getInt("actAddition"));
        data.setDefZhanGong(rs.getLong("defZhanGong"));
        data.setActZhanGong(rs.getLong("actZhanGong"));
        data.setDefDieSoldierNum(rs.getLong("defDieSoldierNum"));
        data.setActDieSoldierNum(rs.getLong("actDieSoldierNum"));
        data.setWinUserId(rs.getLong("winUserId"));
        data.setTime(rs.getDate("time").getTime());
        data.setCityId(rs.getInt("cityId"));
        data.setMatchId(rs.getInt("matchId"));
        return data;
    }

    public Map<Integer,List<CrossTonBingUserWarReport>> getUserReportListMap(int activityId){
        Map<Integer,List<CrossTonBingUserWarReport>> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_c_tonbing_user_report WHERE activityId = ? ORDER BY `time` ASC";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossTonBingUserWarReport data = initUserReportData(rs);
                if(!dataMap.containsKey(data.getMatchId())){
                    dataMap.put(data.getMatchId(),new ArrayList<>());
                }
                dataMap.get(data.getMatchId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }


    public boolean addUserReportList(List<CrossTonBingUserWarReport> reportList) {
        boolean result = false;
        String sql = "INSERT INTO t_c_tonbing_user_report(activityId,no,userIdDef,userIdAct,defPatronsId,actPatronsId," +
                "defSoldierNum,actSoldierNum,defAddition,actAddition,defZhanGong,actZhanGong,defDieSoldierNum,actDieSoldierNum," +
                "winUserId,time,cityId,matchId) " +
                "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        getDb().setConnName(DbNameStrategy.getCross2Db());
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (CrossTonBingUserWarReport data : reportList) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getNo()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getUserIdDef()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getUserIdAct()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getDefPatronsId()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getActPatronsId()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getDefSoldierNum()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getActSoldierNum()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getDefAddition()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getActAddition()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getDefZhanGong()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getActZhanGong()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getDefDieSoldierNum()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getActDieSoldierNum()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getWinUserId()));
            para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(data.getTime())));
            para.put(i++, new DbParameter(Types.INTEGER, data.getCityId()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getMatchId()));
            mapList.add(para);
        }
        if (getDb().executeBatchQuery(sql, mapList) > -1) {
            result = true;
        }
        return result;
    }

}
